package com.example.bgmachine.bgmachine.specifs;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.goods.HomepageGridRightMenuItemData;
import com.example.bgmachine.bgmachine.homes.HomePages;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

public class SpecifPopuView extends Dialog implements
        View.OnClickListener {
    Context context;


    private Boolean bool;
    private View.OnClickListener btnOnClick;

    private TextView spcwcif_popup_name_textview = null;

    private Button spcwcif_popup_close_btn = null;

    private GridView spcwcif_popup_infor_item_gridvew = null;

    public SpecifAdapter mSpecifAdapter = null;
    public List<SpecifData> mSpecifData = null;
    public String mOutInfors ="";


    /**
     * @param @param btnOnClick
     * @return void
     * @throws
     * @Description: 可以自定义button事件
     * @author Sunday
     */
    public void setBtnOnClick(View.OnClickListener btnOnClick) {
        this.btnOnClick = btnOnClick;

    }

    public void setBtnOkClick(String text,
                              View.OnClickListener btnOnClick) {
        this.btnOnClick = btnOnClick;

    }

    public SpecifPopuView(Context context) {
        super(context, R.style.SundayDialogs);
        this.context = context;
        bool = true;


        Log.d("aaa", "aaa===2");
        //create();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spwcif_view_select_dialog);
        hideBottomUIMenu();
        // 设置窗口属性
        if (getWindow() != null) {
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getWindow().setDimAmount(0f);
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            );
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
            );
        }


        init();
        //  setCancel(bool, "");

        Log.d("aaa", "aaa===1");
    }
    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);


        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        Window myWindow = this.getWindow();
        myWindow.setFlags(flag, flag);

        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);


        }

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
            }
        });
    }

    void init() {

        spcwcif_popup_name_textview = findViewById(R.id.spcwcif_popup_name_textview);

        spcwcif_popup_close_btn = findViewById(R.id.spcwcif_popup_close_btn);
        spcwcif_popup_close_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                dismiss();
            }
        });

        spcwcif_popup_infor_item_gridvew = findViewById(R.id.spcwcif_popup_infor_item_gridvew);
        spcwcif_popup_infor_item_gridvew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                SpecifData itema =   mSpecifData.get(position);
                Log.d("aaa", "ContentonItemClick: "+itema.getContent());
                String typeOrder = itema.getTypeOrder();
                String typeinfor = itema.getContent();
                Log.d("aaa", "ContentonItemClicktypeinfor: "+typeinfor);
                dismiss();
                HomePages.mHomePages.setProduceProgress(itema,spcwcif_popup_name_textview.getText().toString()+"-"+itema.getTypename(),typeOrder ,mOutInfors);

            }
        });
        mSpecifData = new LinkedList<SpecifData>();
        mSpecifAdapter = new SpecifAdapter(context, (LinkedList<SpecifData>) mSpecifData);
        if (spcwcif_popup_infor_item_gridvew != null) {
            spcwcif_popup_infor_item_gridvew.setAdapter(mSpecifAdapter);
        }

    }
    public void setOutsInfor(String infors) {
        mOutInfors =infors;
    }
    public void setListInfor(String infors) {
        mSpecifData.clear();
        try {
            JSONArray arrys = new JSONArray(infors);
            for (int i = 0; i < arrys.length(); i++) {
                JSONObject atm = arrys.getJSONObject(i);
                if( atm.isNull("id") || atm.isNull("name") || atm.isNull("typeName") ){
                    continue;
                }
                int typeid = atm.optInt("id");
                String typename = atm.optString("name");
                String typeinfor = atm.optString("typeName");
                String cupTypeReserve2 ="1";// atm.optString("cupTypeReserve2");
                String productCode = "";//atm.optString("productCode");
                String errorData="0";
                if(atm.has("errorData")){
                    errorData= atm.optString("errorData");
                }
 				String price = atm.optString("price");
                JSONArray content =new  JSONArray();// atm.optJSONArray("children");
                mSpecifData.add(new SpecifData(typeid,typename,typeinfor,cupTypeReserve2,productCode,price,errorData,content.toString() ));

            }
        } catch (JSONException e) {
            e.printStackTrace();

        }


        if (mSpecifAdapter != null) {
            mSpecifAdapter.notifyDataSetChanged();
        }
    }

    public void setNameInfor(String infors) {
        if (spcwcif_popup_name_textview != null) {
            spcwcif_popup_name_textview.setText(infors);
        }


    }

    /*
     *
     */
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (isShowing()) {
            //   dismiss();
        }
    }
}
